
import JSEncrypt from 'jsencrypt';

import crypto from "crypto-js";

/***
 *
 * hash 加密解密脚本
 *
 * ***/

const privatekey_groupA = [
    'MIICXQIBAAKBgQDSug4FJ1sLYDc7xUYilp01vgsmQL9X8wpJAarimxNEBuzGQQ8w',
    'RFSSDKGLkjdeJbVcb1yZaDC6PMC3fUMT100JTZa08d2dYUfw5RTru5eHVI3lgAiy',
    'v+8g9TtB9mYbPtN0TSv6PW5bw9ccj5aXy/ECQQCSHUB9OT6fstrzqXb6NTL3ecUr',
    'WbFrIK99Vti31p7RmCve3WAL72pgKTUHrpiEfHH1zB6J',
];

const privateKey_groupB = [
    'SNy+7+7COtY61+XLxPQFly1JTtMJ+kPkJ/dh1aNWPkOaINPjAVZ6CooVOEVDGeVg',
    '6ikdvhH+d17kREnaAjtyQADFiucYOFTm2mgy3VETd158KncBAkEA9ObDw0evB/ON',
    'XHfDcN7ypO8q5E2A3+RxVT3g047SmPc/4g/EwXZAuogKHQ5pNLpe7sKHP9FvAkAF',
];

const privateKey_groupC = [
    '2DX+7KRXUv7gEuDkyq6/wnYmF956aR2gfhrhnlOOGZwjgu0aA6knB28ElwIDAQAB',
    'NKlTldUmaqG0plWeLAOafWc9oI1eW9AwMe0tp1kSVu5/nAVC+eLv8OumYwoQ/4Q0',
    'GwMPV3jAehBl0vyG6WhcnK8W5sVbiAwcoN3kB7LbtVqArQVgqbjvIvVsJTA/15vS',
];

const privateKey_groupD = [
    'AoGBAJkuVnK4oR4r2vq/9LO/+JfQhhpQhsdQDPH1HFuNjvusbQHQ2VGoJY9GqrDW',
    'Klq2t8SBBwJBANxGz7DhLKb77yDCi4b7qg1SXJ9oXWrq35ZTrehWGdqfITSrkxM6',
    'LNzpCwjHeQsF/lx5ohURAkAp4i4Qj8gZpzSPYy+Uvs8gzjNO6bQ6YwVeujlJsD1r',
];

const publicKey_groupA = [
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3w+bvh70Dt2/m7cZnWaJ78A8T',
    'ZIXkjLy9eVwNI5tIiecnUdaKzKZgANW7xvPNoGfayW09CHrYte3JDLSfX4wrkroE',
];

const publicKey_groupB = [
    'ymxSCD4d/o7BhBL1Hs5lFzxJT3CHJNm65Qtj7rfsmye/dkd+wNFLZvqhLlGIuSWf',
    'x8767QbjZaioRIC5lQIDAQAB'
];

const rsa = new JSEncrypt();


// 加密
const DecryptHash=(hash, key)=> {
    const group = [ privatekey_groupA , privateKey_groupB , privateKey_groupC , privateKey_groupD ];
    const privateKey = combineKey(group);
    rsa.setKey(key || privateKey);
    return rsa.decrypt(hash) || '';

}

// const EncryptHash=(data, key)=> {
//
//     const group = [publicKey_groupA, publicKey_groupB];
//     const publicKey = combineKey(group);
//     rsa.setKey(key || publicKey);
//     return rsa.encrypt(data) || '';
//
// }

// 解密
const DecryptHashData = (hash, key, options)=> {

    const defaultOption = {
        mode: crypto.mode.CBC,
        padding: crypto.pad.Pkcs7,
        iv: crypto.enc.Utf8.parse('6367585863675858')
    };

    let hashKey = crypto.enc.Utf8.parse(key);

    return crypto.AES.decrypt(hash, hashKey, options || defaultOption).toString(crypto.enc.Utf8);

}

const combineKey=(key)=> {
    const maxLoop = key[0].length;
    const finalGroup = [];
    for (let i = 0; i < maxLoop; i++) {
        for (const group of key) {
            const part = group[i];
            if (part){
                finalGroup.push(part);
            }
        }
    }
    return finalGroup.join('\n');
}


export {
    DecryptHashData,
    DecryptHash,
  //  EncryptHash
}